package com.heima.xxljob.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class HelloJob {

    @XxlJob("demoJobHandler")
    public void hellojob(){
        System.out.println("简单任务执行。。。。"+new Date());
    }

    @XxlJob("shardingJobHandler")
    public void shardingJob(){

        //总分片数，当前分片
        int shardTotal = XxlJobHelper.getShardTotal();
        int shardIndex = XxlJobHelper.getShardIndex();

        //执行业务
        List<Integer> list = getList();
        for (Integer integer : list) {
            if(integer % shardTotal == shardIndex){
                //具体的任务执行了
                System.out.println("当前分片："+shardIndex+",执行了任务项为："+integer);
            }
        }


    }

    public List<Integer> getList(){

        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }

        return list;
    }
}
